package Interface;

import ulht.ia.*;
import java.util.*;
import javax.swing.*;
import sun.org.mozilla.javascript.internal.regexp.SubString;

public class Home extends javax.swing.JFrame {
    
    public Home() {
        initComponents();
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        menuBar1 = new java.awt.MenuBar();
        menu1 = new java.awt.Menu();
        menu2 = new java.awt.Menu();
        GenomasText = new javax.swing.JTextField();
        GenomasLabel = new javax.swing.JLabel();
        NSalaText = new javax.swing.JTextField();
        CodSalaText = new javax.swing.JTextField();
        NSalaLabel = new javax.swing.JLabel();
        CodSalaLabel = new javax.swing.JLabel();
        AddSalaButton = new javax.swing.JButton();
        NDiscLabel = new javax.swing.JLabel();
        CodDiscLabel = new javax.swing.JLabel();
        CodDiscText = new javax.swing.JTextField();
        NDiscText = new javax.swing.JTextField();
        AddDiscButton = new javax.swing.JButton();
        TaxaMutacaoText = new javax.swing.JTextField();
        TurmasLabel1 = new javax.swing.JLabel();
        NTurmaText = new javax.swing.JTextField();
        CodTurmaText = new javax.swing.JTextField();
        NTurmaLabel = new javax.swing.JLabel();
        CodTurmaLabel = new javax.swing.JLabel();
        AddTurmaButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        GeneratorButton = new javax.swing.JButton();
        ListaSalas = new java.awt.List();
        ListaDisciplinas = new java.awt.List();
        ListaTurmas = new java.awt.List();
        TipoCrossover = new javax.swing.JComboBox();
        NTurmaLabel1 = new javax.swing.JLabel();

        menu1.setLabel("File");
        menuBar1.add(menu1);

        menu2.setLabel("Edit");
        menuBar1.add(menu2);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        GenomasText.setText("10");

        GenomasLabel.setText("Nº Genormas");

        NSalaText.setText("01");

        CodSalaText.setText("Sala");

        NSalaLabel.setText("Descrição");

        CodSalaLabel.setText("Código Sala");

        AddSalaButton.setText("Adicionar Sala");
        AddSalaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddSalaButtonActionPerformed(evt);
            }
        });

        NDiscLabel.setText("Nº Disciplina");

        CodDiscLabel.setText("Descrição");

        CodDiscText.setText("Disciplina");

        NDiscText.setText("01");

        AddDiscButton.setText("Adicionar Disciplina");
        AddDiscButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddDiscButtonActionPerformed(evt);
            }
        });

        TaxaMutacaoText.setText("0.1");
        TaxaMutacaoText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TaxaMutacaoTextActionPerformed(evt);
            }
        });

        TurmasLabel1.setText("Taxa Mutação");

        NTurmaText.setText("01");
        NTurmaText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NTurmaTextActionPerformed(evt);
            }
        });

        CodTurmaText.setText("Turma");

        NTurmaLabel.setText("Nº Turma");

        CodTurmaLabel.setText("Descrição");

        AddTurmaButton.setText("Adicionar Turma");
        AddTurmaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddTurmaButtonActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setText("Sistema Geração de Horários");
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        GeneratorButton.setText("Gerar Horários");
        GeneratorButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                GeneratorButtonMouseClicked(evt);
            }
        });
        GeneratorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GeneratorButtonActionPerformed(evt);
            }
        });

        TipoCrossover.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "OX", "PMX", "CX" }));

        NTurmaLabel1.setText("Tipo Crossover");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(TurmasLabel1)
                    .addComponent(NSalaLabel)
                    .addComponent(CodSalaLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(TaxaMutacaoText)
                    .addComponent(AddSalaButton, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
                    .addComponent(ListaSalas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(CodSalaText)
                    .addComponent(NSalaText))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(GenomasLabel)
                            .addComponent(NDiscLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addComponent(CodDiscLabel)
                        .addGap(12, 12, 12)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(CodDiscText, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                    .addComponent(AddDiscButton, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                    .addComponent(ListaDisciplinas, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                    .addComponent(NDiscText)
                    .addComponent(GenomasText))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(NTurmaLabel)
                    .addComponent(CodTurmaLabel)
                    .addComponent(NTurmaLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(ListaTurmas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(NTurmaText)
                    .addComponent(CodTurmaText)
                    .addComponent(AddTurmaButton, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
                    .addComponent(TipoCrossover, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE)
                .addComponent(GeneratorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(TaxaMutacaoText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(TurmasLabel1)
                            .addComponent(GenomasText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(GenomasLabel)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(TipoCrossover, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(NTurmaLabel1))))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(NSalaLabel)
                        .addGap(44, 44, 44)
                        .addComponent(GeneratorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(NSalaText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(CodSalaLabel))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(NDiscText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(NDiscLabel)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(CodDiscText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(CodSalaText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(CodDiscLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(AddDiscButton)
                                    .addComponent(AddSalaButton)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(NTurmaText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(NTurmaLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(CodTurmaText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(CodTurmaLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(AddTurmaButton)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(ListaTurmas, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                            .addComponent(ListaSalas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(ListaDisciplinas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGap(56, 56, 56))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void GeneratorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GeneratorButtonActionPerformed
                
        Timetable janela = new Timetable(this,true);        
        Salas salas = new Salas();
        Disciplinas disciplinas = new Disciplinas();
        Turmas turmas = new Turmas();
        
        //Inserir Salas no Objecto - Caso a lista não esteja vazia
        if (ListaSalas.getItemCount()>0)
        {
            for (int i=0;i< ListaSalas.getItemCount();i++)
            {
                int length =  ListaSalas.getItem(i).length();

                //Recolhe valor da dropdown, separa o código da sala, do seu nome e adiciona ao objecto.
                salas.addSala(ListaSalas.getItem(i).substring(0, ListaSalas.getItem(i).indexOf("-")),ListaSalas.getItem(i).substring(ListaSalas.getItem(i).indexOf("-")+2,length));
            }
        }
                
        //Inserir Disciplinas no Objecto - Caso a lista não esteja vazia
        if (ListaDisciplinas.getItemCount()>0)
        {
            for (int i=0;i< ListaDisciplinas.getItemCount();i++)
            {
                int length =  ListaDisciplinas.getItem(i).length();

                //Recolhe valor da dropdown, separa o código da Disciplina, do seu nome e adiciona ao objecto.
                disciplinas.addDisciplina(ListaDisciplinas.getItem(i).substring(0, ListaDisciplinas.getItem(i).indexOf("-")),ListaDisciplinas.getItem(i).substring(ListaDisciplinas.getItem(i).indexOf("-")+2,length));
            }
        }
        
        //Inserir Turmas no Objecto - Caso a lista não esteja vazia
        if (ListaTurmas.getItemCount()>0)
        {
            //Inserir Turmas no Objecto
            for (int i=0;i< ListaTurmas.getItemCount();i++)
            {
                int length =  ListaTurmas.getItem(i).length();

                //Recolhe valor da dropdown, separa o código da Disciplina, do seu nome e adiciona ao objecto.
                turmas.addTurma(ListaTurmas.getItem(i).substring(0, ListaTurmas.getItem(i).indexOf("-")),ListaTurmas.getItem(i).substring(ListaTurmas.getItem(i).indexOf("-")+2,length));
            }
        }     
        
        Engine engine = new Engine(salas.getListaSalas(), turmas.getListaTurmas(), disciplinas.getListaDisciplinas(),Integer.parseInt(GenomasText.getText().toString()), TipoCrossover.getSelectedIndex(),Double.parseDouble(TaxaMutacaoText.getText().toString()));

        janela.setEngine(engine);
        
        //Apresenta o ecrã onde vai ser mostrado o horário
        janela.setLocationRelativeTo(this);
        janela.setVisible(true);
        
    }//GEN-LAST:event_GeneratorButtonActionPerformed

    private void GeneratorButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_GeneratorButtonMouseClicked

    }//GEN-LAST:event_GeneratorButtonMouseClicked

    private void AddSalaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddSalaButtonActionPerformed
        
        Salas sala = new Salas();
        sala.addSala(CodSalaText.getText().toString(), NSalaText.getText().toString());
        
        ListaSalas.add(NSalaText.getText() + " - " + CodSalaText.getText());
        CodSalaText.setText("");
        NSalaText.setText("");
    }//GEN-LAST:event_AddSalaButtonActionPerformed

    private void AddDiscButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddDiscButtonActionPerformed
        Disciplinas disciplina = new Disciplinas();
        disciplina.addDisciplina(CodDiscText.getText().toString(), NDiscText.getText().toString());
        
        ListaDisciplinas.add(NDiscText.getText() + " - " + CodDiscText.getText());
        CodDiscText.setText("");
        NDiscText.setText("");       
    }//GEN-LAST:event_AddDiscButtonActionPerformed

    private void AddTurmaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddTurmaButtonActionPerformed
        Turmas turma = new Turmas();
        turma.addTurma(CodTurmaText.getText().toString(), NTurmaText.getText().toString());
            
        ListaTurmas.add(NTurmaText.getText() + " - " + CodTurmaText.getText());
        CodTurmaText.setText("");
        NTurmaText.setText("");
    }//GEN-LAST:event_AddTurmaButtonActionPerformed

    private void NTurmaTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NTurmaTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_NTurmaTextActionPerformed

    private void TaxaMutacaoTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TaxaMutacaoTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TaxaMutacaoTextActionPerformed

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Home().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddDiscButton;
    private javax.swing.JButton AddSalaButton;
    private javax.swing.JButton AddTurmaButton;
    private javax.swing.JLabel CodDiscLabel;
    private javax.swing.JTextField CodDiscText;
    private javax.swing.JLabel CodSalaLabel;
    private javax.swing.JTextField CodSalaText;
    private javax.swing.JLabel CodTurmaLabel;
    private javax.swing.JTextField CodTurmaText;
    private javax.swing.JButton GeneratorButton;
    private javax.swing.JLabel GenomasLabel;
    private javax.swing.JTextField GenomasText;
    private java.awt.List ListaDisciplinas;
    private java.awt.List ListaSalas;
    private java.awt.List ListaTurmas;
    private javax.swing.JLabel NDiscLabel;
    private javax.swing.JTextField NDiscText;
    private javax.swing.JLabel NSalaLabel;
    private javax.swing.JTextField NSalaText;
    private javax.swing.JLabel NTurmaLabel;
    private javax.swing.JLabel NTurmaLabel1;
    private javax.swing.JTextField NTurmaText;
    private javax.swing.JTextField TaxaMutacaoText;
    private javax.swing.JComboBox TipoCrossover;
    private javax.swing.JLabel TurmasLabel1;
    private javax.swing.JLabel jLabel1;
    private java.awt.Menu menu1;
    private java.awt.Menu menu2;
    private java.awt.MenuBar menuBar1;
    // End of variables declaration//GEN-END:variables
}
